package com.core.tag.impl;

import java.io.IOException;

import java.util.List;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.core.util.DspUtil;
import com.dsp.bean.Menu;
import com.dsp.services.MenuService;

/**
 * 菜单自定义标签
 * 
 * @author laker
 * 
 */
public class MenuTag extends TagSupport {

	private String menu = "";

	private String subMenu = "";

	private MenuService ms;

	/**
	 * @return the current
	 */
	public String getMenu() {
		return menu;
	}

	/**
	 * @param current
	 *            the current to set
	 */
	public void setMenu(String current) {
		this.menu = current;
	}

	/**
	 * @return the subMenu
	 */
	public String getSubMenu() {
		return subMenu;
	}

	/**
	 * @param subMenu
	 *            the subMenu to set
	 */
	public void setSubMenu(String subMenu) {
		this.subMenu = subMenu;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -8258963398264213387L;

	public int doStartTag() {
		JspWriter out = pageContext.getOut();
		StringBuffer menu = new StringBuffer(500);
		ms = (MenuService) DspUtil.getBean("menuService");
		StringBuffer subString = new StringBuffer(200);
		if ("".equals(subMenu) == false) {
			String parentId = this.getParentId();
			DetachedCriteria subDC = DetachedCriteria.forClass(Menu.class);
			subDC.add(Restrictions.eq("parent", parentId));
			subString = this.getCurrentMenu(subDC);
			menu.append(this.getParentMenu(false));
			menu.append(subString);
		} else {
			menu.append(this.getParentMenu(true));
		}
		try {
			out.println(menu.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (SKIP_BODY);
	}

	public StringBuffer getParentMenu(boolean isContain) {
		StringBuffer menu = new StringBuffer(200);
		DetachedCriteria dc = DetachedCriteria.forClass(Menu.class);
		dc.add(Restrictions.eq("parent", "0"));
		List<Menu> list = ms.likeBy(dc);// ms.getCondition(null);//p.getList();
		menu.append("<div class=\"grid_16\" id=\"header\">\n");
		menu.append("<div id='menu'>\n");
		menu.append("<ul class='group' id='menu_group_main'>\n");
		Menu m = null;
		int count = list.size();
		int $$current = -1;
		String messageBox = "";
		for (int i = 0; i < count; i++) {
			m = list.get(i);
			if (m.getSelected().equals(getMenu())) {
				$$current = i;
			}
			if ("newsletter".equalsIgnoreCase(m.getIcon())) {
				messageBox = "<div id=\"id_messagebox\" class='messagebox'><span id=\"showmessage_icon\" class=\"showcount\"></span></div>";
			} else{
				messageBox = "";
			}
			menu.append("<li class='item " + (i == 0 ? "first" : (i == count - 1) ? "last" : "middle") + "' id='" + m.getOrder() + "'>\n");
			menu.append("<a href='" + m.getEvent() + "' class='main " + (m.getSelected().equals(getMenu()) ? "current" : "") + "' >\n");
			menu.append("<span class='outer'><span class='inner " + m.getIcon() + "'>" + messageBox + m.getTag() + "</span></span>\n");
			menu.append("</a></li>\n");
		}

		menu.append("</ul></div>\n");
		menu.append("</div>\n");
		if (isContain) {
			// sub navigation
			if ($$current > -1) {
				m = list.get($$current);
				DetachedCriteria subDC = DetachedCriteria.forClass(Menu.class);
				subDC.add(Restrictions.eq("parent", m.getId().toString()));
				StringBuffer _menu = this.getCurrentMenu(subDC);
				menu.append(_menu);
			} else {
				menu.append("<div class=\"grid_16\">\n");
				menu.append("<!-- TABS START -->\n");
				menu.append("<div id='tabs'>\n");
				menu.append("<div class='container'>\n");
				menu.append("<ul>\n");
				menu.append("<li><a href='#' class=''><span></span></a></li>\n");
				menu.append("</ul>\n").append("</div></div></div>\n");
			}
		}
		return menu;
	}

	/**
	 * 二级菜单
	 * 
	 * @param subDC
	 * @return
	 */
	public StringBuffer getCurrentMenu(DetachedCriteria subDC) {
		StringBuffer menu = new StringBuffer(200);
		/**
		 * DetachedCriteria subDC = DetachedCriteria.forClass(Menu.class); subDC.add(Restrictions.like("event", subMenu+"%"));
		 **/
		List<Menu> subList = ms.likeBy(subDC);
		menu.append("<div class=\"grid_16\">\n");
		menu.append("<!-- TABS START -->\n");
		menu.append("<div id='tabs'>\n");
		menu.append("<div class='container'>\n");
		menu.append("<ul>\n");
		Menu _subMenu = null;
		for (int j = 0; j < subList.size(); j++) {
			_subMenu = subList.get(j);
			this.menu = _subMenu.getParent();
			String curr = "";
			if ("".equals(this.getSubMenu()) && j == 0){
				curr = "current";
			} 
			if (!"".equals(this.getSubMenu()) && _subMenu.getEvent().indexOf(this.getSubMenu()) > -1){
				curr = "current";
			}
			menu.append("<li><a href='" + _subMenu.getEvent() + "' class='"+ curr +"'><span>" + _subMenu.getTag() + "</span></a></li>\n");
		}
		menu.append("</ul>\n").append("</div></div></div>\n");
		return menu;
	}
	/**
	 * 查询子菜单对应的父菜单
	 * @return
	 */
	public String getParentId(){
		DetachedCriteria subDC = DetachedCriteria.forClass(Menu.class);
		subDC.add(Restrictions.like("event", subMenu + "%"));
		subDC.add(Restrictions.gt("parent", "0"));
		List<Menu> l = ms.likeBy(subDC);
		return l.get(0).getParent();
	}
}

/**
 * <div class="grid_16"> <!-- TABS START --> <div id="tabs"> <div class="container">
 * <ul>
 * <li><a href="users" class="current"><span>Dashboard elements</span> </a></li>
 * <li><a href="companys"><span>Content Editing</span> </a></li>
 * <li><a href="#"><span>Submenu Link 3</span> </a></li>
 * <li><a href="#"><span>Submenu Link 4</span> </a></li>
 * 
 * <li><a href="#"><span>Submenu Link 5</span> </a></li>
 * <li><a href="#"><span>Submenu Link 6</span> </a></li>
 * <li><a href="#" class="more"><span>More Submenus</span> </a></li>
 * </ul>
 * </div> </div> <!-- TABS END --> </div> <div id="menu">
 * <ul class="group" id="menu_group_main">
 * <li class="item first" id="one"><a href="#" class="main current"> <span class="outer"> <span class="inner dashboard"><fmt:message
 * key="dsp.navigation.dashboard" /> </span> </span> </a></li>
 * <li class="item middle" id="two"><a href="#" class="main"><span class="outer"><span class="inner content"><fmt:message
 * key="dsp.navigation.company" /> </span> </span> </a></li>
 * <li class="item middle" id="three"><a href="#"><span class="outer"><span class="inner reports png"><fmt:message
 * key="dsp.navigation.report" /> </span> </span> </a></li>
 * 
 * <li class="item middle" id="four"><a href="#" class="main"><span class="outer"><span class="inner users">Users</span> </span> </a></li>
 * <li class="item middle" id="five"><a href="#" class="main"><span class="outer"><span class="inner media_library">Media Library</span>
 * </span> </a></li>
 * <li class="item middle" id="six"><a href="#" class="main"><span class="outer"><span class="inner event_manager">Event Manager</span>
 * </span> </a></li>
 * <li class="item middle" id="seven"><a href="#" class="main"><span class="outer"><span class="inner newsletter">Newsletter</span> </span>
 * </a></li>
 * <li class="item last" id="eight"><a href="#" class="main"><span class="outer"><span class="inner settings">Settings</span> </span> </a></li>
 * </ul>
 * </div>
 * 
 * 
 */
